<?php
/**
 * 在模板中取系统选项中的值
 * @param array $option
 * @param mixed $compiler
 * @return string
 */
function smarty_modifiercompiler_option($option, $compiler) {
	if (count ( $option ) < 1) {
		return "'error usage of option'";
	}
	$default = isset ( $option [1] ) ? $option [1] : "''";
	$output = "CoreOptionModel::getOption({$option[0]},{$default})";
	return $output;
}
/**
 * 在模板中显示代码片断
 *
 * @param array $args        	
 * @param mixed $compiler        	
 * @return string
 */
function smarty_modifiercompiler_fragment($args, $compiler) {
	if (count ( $args ) < 1) {
		return "'error usage of fragment'";
	}
	$default = isset ( $args [1] ) ? $args [1] : "''";
	$output = "show_fragment({$args[0]},$default)";
	return $output;
}
//显示代码片断
function show_fragment($name, $default) {
	static $fM = false;
	if (! $fM) {
		$fM = new WebFragmentModel ();
	}
	$frag = $fM->get ( 'fragment', array ('name' => $name ) );
	$frag = $frag ? htmlspecialchars_decode ( $frag ) : $default;
	return $frag;
}
// 发布栏目页面时
function on_after_save_page_of_category($page) {
	if ($page ['page_mode'] == 'category' && ! empty ( $page ['category'] )) { // 是分类时
		$cM = new WebPageCategory ();
		$where ['name'] = $page ['category'];
		$data ['page_id'] = $page ['page_id'];
		$data ['url'] = $page ['url'];
		$cM->update ( $data, $where );
	}
}
bind ( 'on_after_save_page', 'on_after_save_page_of_category' );
// 初始化默认的栏目页
function get_default_page_for_category($page) {
	if ($page ['page_mode'] == 'category') {
		$cM = new WebPageCategory ();
		$cate = $cM->where ( array ('name' => $page ['category'] ) )->retrieve ( '*', 0 );
		if ($cate) {
			$page ['title'] = $cate ['title'];
			$page ['category_name'] = $cate ['title'];
			$page ['url'] = $cate ['url_path'] . '/index.html';
		}
	}
	return $page;
}
bind ( 'get_default_page', 'get_default_page_for_category' );
//填充普通页面的分页信息
function _core_encapsulated_page($page) {
	if ($page ['page_mode'] == 'default') {
		$contents = explode('_baidu_page_break_tag_', $page ['content'] );
		$page ['totalPage'] = count ( $contents );
		$cur = $page ['cpage'] - 1;
		$page ['content'] = $contents [$cur];
	}
	return $page;
}
bind ( 'encapsulated_page', '_core_encapsulated_page' );
//当删除页面时
function on_delete_page_category($rst, $ids) {
	$cm = new WebPageCategory ();
	$cm->update ( array ('url' => '', 'page_id' => 0 ), array ('page_id IN' => $ids ) );
	return $rst;
}
bind ( 'on_delete_page', 'on_delete_page_category', 10, 2 );
// 内置频道栏目
function _the_buildin_category_types($types){
	$types['page'] = '频道栏目';
	return $types;
}
bind('get_category_types','_the_buildin_category_types');
// 权限注册
function _core_rbac_resources($resources) {
	$user ['id'] = 'user';
	$user ['name'] = '用户账户与用户组';
	$user ['operations'] [] = array ('name' => '查看用户', 'op' => 'view' );
	$user ['operations'] [] = array ('name' => '新增用户', 'op' => 'add' );
	$user ['operations'] [] = array ('name' => '修改用户', 'op' => 'update' );
	$user ['operations'] [] = array ('name' => '删除用户', 'op' => 'delete' );
	$user ['operations'] [] = array ('name' => '用户授权', 'op' => 'grant' );
	$user ['operations'] [] = array ('name' => '查看用户组', 'op' => 'viewgroup' );
	$user ['operations'] [] = array ('name' => '编辑用户组', 'op' => 'delgroup' );
	$user ['operations'] [] = array ('name' => '删除用户组', 'op' => 'delgroup' );
	$resources [] = $user;
	
	$theme ['id'] = 'theme';
	$theme ['name'] = '主题与模板管理';
	$theme ['operations'] [] = array ('name' => '查看列表', 'op' => 'view' );
	$theme ['operations'] [] = array ('name' => '新增主题', 'op' => 'add' );
	$resources [] = $theme;
	
	$page ['id'] = 'page';
	$page ['name'] = '网页管理';
	$page ['operations'] [] = array ('name' => '发布', 'op' => 'publish' );
	$page ['operations'] [] = array ('name' => '删除', 'op' => 'delete' );
	$resources [] = $page;
	
	return $resources;
}